/** Author: David Powell
This file produces Table D1
This file takes a while to run
**/


clear all
set more off
set mat 800
set seed 8721

global dir "/jules/b/dpowell"
global DATA "${dir}/purdue/replication/DATA"
global OUTPUT "${dir}/purdue/replication/output"


use mortalitydata_qtr

gen overdose_rate=100000*(overdose/totpop)
gen opioid_rate=100000*opioids/totpop

**create quarterly variables
gen str tmp=string(year)
gen str tmp2=string(quarter)
replace tmp=tmp+"-"+tmp2
gen dt=quarterly(tmp, "YQ", 2050)
format %tqCY dt
drop tmp*
egen ggg=group(year quarter)
keep if ggg<.

sum ggg if year==1996
local first=r(min)
local treat_y2=`first'-1

tsset stf ggg


***results are stored in these matrices***
matrix AAA=J(1000,3,.)
matrix BBB=J(1000,3,.)
matrix CCC=J(1000,3,.)
matrix DDD=J(1000,3,.)

gen tripl=1-nontripl

qui foreach aaa of numlist 1/1000 {


**randomize treatment
gen tmp=uniform() if year==1983 & quarter==1
egen tmp2=rank(tmp), by(tripl year quarter)
levelsof stf if tmp2<6 & tripl==0, local(sss)


if (`aaa'==1) {
	levelsof stf if tripl==1, local(sss)
}

gen faketripl=0
foreach nnn of numlist `sss' {
	replace faketripl=1 if stf==`nnn'
}
drop tmp*

local res= ""

foreach jjj of numlist 1/`treat_y2' {
	local res  `"`res'   overdose_rate(`jjj')"'
}


gen diff=.

 foreach nnn of numlist `sss' {


	levelsof stf if faketripl==0, local(donors)
	synth overdose_rate `res', trunit(`nnn') trperiod(`first') counit(`donors') 
	matrix A2=e(Y_synthetic)
	matrix B2=e(Y_treated)
	matrix C2=B2-A2
	svmat C2

	foreach nnn2 of numlist 1/140 {
		gen tmp=C2 if _n==`nnn2'
		egen tmp2=max(tmp)
		replace diff=tmp2 if stf==`nnn' & ggg==`nnn2'
		drop tmp*

	}
	drop C* 

}


egen beta=mean(4*diff), by(ggg)
sum beta if year>1995 & year<2001 
matrix AAA[`aaa',1]=r(mean)
sum beta if year>2000 & year<2011 
matrix AAA[`aaa',2]=r(mean)
sum beta if year>2010 & year<2018  
matrix AAA[`aaa',3]=r(mean)

egen beta2=wtmean(4*diff), by(ggg) weight(totpop)
sum beta2 if year>1995 & year<2001 
matrix BBB[`aaa',1]=r(mean)
sum beta2 if year>2000 & year<2011 
matrix BBB[`aaa',2]=r(mean)
sum beta2 if year>2010 & year<2018  
matrix BBB[`aaa',3]=r(mean)
drop faketri-beta2

}


set seed 8721
***OPIOID OVERDOSES
qui foreach aaa of numlist 1/1000 {

gen tmp=uniform() if year==1983 & quarter==1
egen tmp2=rank(tmp), by(tripl year quarter)

levelsof stf if tmp2<6 & tripl==0, local(sss)


if (`aaa'==1) {
	levelsof stf if tripl==1, local(sss)
}

gen faketripl=0
foreach nnn of numlist `sss' {
	replace faketripl=1 if stf==`nnn'
}
drop tmp*

local res= ""

foreach jjj of numlist 1/`treat_y2' {
	local res  `"`res'   opioid_rate(`jjj')"'
}


gen diff=.
qui foreach nnn of numlist `sss' {


	levelsof stf if faketripl==0, local(donors)
	synth opioid_rate `res', trunit(`nnn') trperiod(`first') counit(`donors') 
	matrix A2=e(Y_synthetic)
	matrix B2=e(Y_treated)
	matrix C2=B2-A2
	svmat C2

	foreach nnn2 of numlist 1/140 {
		gen tmp=C2 if _n==`nnn2'
		egen tmp2=max(tmp)
		replace diff=tmp2 if stf==`nnn' & ggg==`nnn2'
		drop tmp*

	}
	drop C* 

}


egen beta=mean(4*diff), by(ggg)
sum beta if year>1995 & year<2001 
matrix CCC[`aaa',1]=r(mean)
sum beta if year>2000 & year<2011 
matrix CCC[`aaa',2]=r(mean)
sum beta if year>2010 & year<2018  
matrix CCC[`aaa',3]=r(mean)

egen beta2=wtmean(4*diff), by(ggg) weight(totpop)
sum beta2 if year>1995 & year<2001 
matrix DDD[`aaa',1]=r(mean)
sum beta2 if year>2000 & year<2011 
matrix DDD[`aaa',2]=r(mean)
sum beta2 if year>2010 & year<2018  
matrix DDD[`aaa',3]=r(mean)

drop faketri-beta2

}


clear
svmat AAA
svmat BBB
svmat CCC
svmat DDD


foreach var of varlist AAA* BBB* CCC* DDD* {
	egen rk_`var'=rank(`var')
}

***RESULTS***
sum rk* AAA* BBB* CCC* DDD* if _n==1

